package cn.hahah.module.blog.service.userdialoguegroup;

import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.hahah.module.blog.controller.admin.userdialoguegroup.vo.*;
import cn.hahah.module.blog.dal.dataobject.userdialoguegroup.UserDialogueGroupDO;
import cn.hahah.framework.common.pojo.PageResult;
import cn.hahah.framework.common.pojo.PageParam;
import cn.hahah.framework.common.util.object.BeanUtils;

import cn.hahah.module.blog.dal.mysql.userdialoguegroup.UserDialogueGroupMapper;

import static cn.hahah.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.hahah.module.blog.enums.ErrorCodeConstants.*;

/**
 * 用户对话分组 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class UserDialogueGroupServiceImpl implements UserDialogueGroupService {

    @Resource
    private UserDialogueGroupMapper userDialogueGroupMapper;

    @Override
    public Long createUserDialogueGroup(UserDialogueGroupSaveReqVO createReqVO) {
        // 插入
        UserDialogueGroupDO userDialogueGroup = BeanUtils.toBean(createReqVO, UserDialogueGroupDO.class);
        userDialogueGroupMapper.insert(userDialogueGroup);
        // 返回
        return userDialogueGroup.getId();
    }

    @Override
    public void updateUserDialogueGroup(UserDialogueGroupSaveReqVO updateReqVO) {
        // 校验存在
        validateUserDialogueGroupExists(updateReqVO.getId());
        // 更新
        UserDialogueGroupDO updateObj = BeanUtils.toBean(updateReqVO, UserDialogueGroupDO.class);
        userDialogueGroupMapper.updateById(updateObj);
    }

    @Override
    public void deleteUserDialogueGroup(Long id) {
        // 校验存在
        validateUserDialogueGroupExists(id);
        // 删除
        userDialogueGroupMapper.deleteById(id);
    }

    private void validateUserDialogueGroupExists(Long id) {
        if (userDialogueGroupMapper.selectById(id) == null) {
            throw exception(USER_DIALOGUE_GROUP_NOT_EXISTS);
        }
    }

    @Override
    public UserDialogueGroupDO getUserDialogueGroup(Long id) {
        return userDialogueGroupMapper.selectById(id);
    }

    @Override
    public PageResult<UserDialogueGroupDO> getUserDialogueGroupPage(UserDialogueGroupPageReqVO pageReqVO) {
        return userDialogueGroupMapper.selectPage(pageReqVO);
    }

}